using QFramework;
using UnityEngine;
using System;
using DG.Tweening;
using System.Collections.Generic;

namespace HareGame
{
    public class CameraManager : MonoBehaviour_Ex, ISingleton
    {
        [Header("相机定位移动速度")]
        [SerializeField]
        private float _moveSpeed = 150;

        [Header("相机可视范围")]
        private float _size = 15;

        public bool IsEnableCameraTouch => _cameraTouch.enabled;
        public bool IsEnableCameraMove => _cameraTouch.enabled;


        private Camera _camera;
        private Dictionary<string, Vector3> _cameraPos;
        private Dictionary<string, float> _cameraFOV;
        private MobileTouchCamera _cameraTouch;

        private int _cullingMask;

        /// <summary>
        /// 初始化
        /// </summary>
        public void OnSingletonInit()
        {
            _camera = GetComponent<Camera>();
            _cameraTouch = GetComponent<MobileTouchCamera>();
            _cameraPos = new Dictionary<string, Vector3>();
            _cameraFOV = new Dictionary<string, float>();

            _cullingMask = Camera.main.cullingMask;
        }

        /// <summary>
        /// 全屏弹窗时建议调用
        /// </summary>
        /// <param name="isCulling">是否剔除</param>
        public void SetCullingMask(bool isCulling)
        {
            Camera.main.cullingMask = isCulling ? 0 : _cullingMask;
        }

        /// <summary>
        /// 定义单例
        /// </summary>
        public static CameraManager Instance
        {
            get { return MonoSingletonProperty<CameraManager>.Instance; }
        }

        /// <summary>
        /// 移动相机
        /// </summary>
        /// <param name="pos">坐标</param>
        /// <param name="onReach">到达回调</param>
        public string Move2Pos(Vector3 pos, Action onReach = null)
        {
            string guid = Guid.NewGuid().ToString();
            _cameraPos.Add(guid, transform.position);
            _cameraFOV.Add(guid, _camera.orthographicSize);
            _camera.DOKill();
            transform.DOKill();
            EnableCameraTouch(false);
            transform.DOMove(pos, _moveSpeed).SetSpeedBased(true).SetEase(Ease.InOutQuad).OnComplete(() =>
            {
                EnableCameraTouch(true);
                onReach?.Invoke();
            }).SetUpdate(true);
            return guid;
        }

        /// <summary>
        /// 移动相机
        /// </summary>
        /// <param name="pos">坐标</param>
        /// <param name="size">相机广角</param>
        /// <param name="onReach">到达回调</param>
        public string Move2Pos(Vector3 pos, float size, Action onReach = null)
        {
            _camera.DOOrthoSize(size, Vector3.Distance(pos, transform.position) / _moveSpeed).SetEase(Ease.InOutQuad).SetUpdate(true);
            return Move2Pos(pos, onReach);
        }

        /// <summary>
        /// 移动相机(使用_size作为广角)
        /// </summary>
        /// <param name="pos">pos</param>
        /// <param name="onReach">onReach</param>
        /// <returns></returns>
        public string Move2PosWithDefaultSize(Vector3 pos, Action onReach = null)
        {
            _camera.DOOrthoSize(_size, Vector3.Distance(pos, transform.position) / _moveSpeed).SetEase(Ease.InOutQuad).SetUpdate(true);
            return Move2Pos(pos, onReach);
        }

        /// <summary>
        /// 返回
        /// </summary>
        /// <param name="Guid">编号</param>
        public void Back4Building(string Guid)
        {
            EnableCameraTouch(false);
            _camera.DOKill();
            transform.DOKill();
            _camera.DOOrthoSize(_cameraFOV[Guid], Vector3.Distance(_cameraPos[Guid], transform.position) / _moveSpeed).SetEase(Ease.InOutQuad);
            transform.DOMove(_cameraPos[Guid], _moveSpeed).SetSpeedBased(true).SetEase(Ease.InOutQuad).OnComplete(() =>
            {
                EnableCameraTouch(true);
                _cameraPos.Remove(Guid);
                _cameraFOV.Remove(Guid);
            });
        }

        /// <summary>
        /// 是否能控制相机
        /// </summary>
        /// <param name="enable">是否启用</param>
        public void EnableCameraTouch(bool enable)
        {
            _cameraTouch.enabled = enable;
        }

        public void BigSize()
        {
            _cameraTouch.CamZoomMin = 2;
            _cameraTouch.CamZoomMax = 150;
            _cameraTouch.BoundaryMax = new Vector2(1000, 1000);
            _cameraTouch.BoundaryMin = new Vector2(-1000, -1000);
        }

        public void ResetSize()
        {
            _cameraTouch.CamZoomMin = 10;
            _cameraTouch.CamZoomMax = 20;
            _cameraTouch.BoundaryMax = new Vector2(100, 120);
            _cameraTouch.BoundaryMin = new Vector2(-120, -100);
        }

        #region 相机跟随
        private Transform _follow;

        private Vector3 FollowOffset;

        public void FollowObj(Transform target, Vector3 offset, float size = 20)
        {
            _follow = null;
            if (target == null)
            {
                return;
            }

            FollowOffset = offset;
            _camera.orthographicSize = size;
            _follow = target;
            EnableCameraTouch(false);
        }

        private void LateUpdate()
        {
            if (_follow != null)
            {
                transform.position = new Vector3(_follow.position.x, _camera.transform.position.y, _follow.position.z) + FollowOffset;
            }
        }
        #endregion
    }
}